/**
 * Created by Administrator on 2017/12/26.
 */

$('#channels').select2();
/**
 * 初始化视频详情对话框
 */
var ChannelItemInfoDlg = {
    ChanelItemInfoData: {},
    validateFields: {
    }
};

/**
 * 清除数据
 */
ChannelItemInfoDlg.clearData = function() {
    this.ChanelItemInfoData = {};
}

ChannelItemInfoDlg.radioSearch = function() {
  var keyword = $("#keyword").val();
  $.ajax({
      type: "GET",
      url:  Feng.ctxPath + "/channel/item/radio/list",
      data: {"keyword":keyword} ,
      success:function (result) {
          $("#radios").empty();
          for (var i = 0; i < result.length;i++) {
              $("#radios").append("<input class='radios' name='radio_name' value='"+result[i].id+"' age-name='"+result[i].name+"' type='checkbox'/>"+result[i].name+"&nbsp;&nbsp;&nbsp;&nbsp;")
          }
      }
  });
};

/**
 * 设置对话框中的数据
 *
 * @param key 数据的名称
 * @param val 数据的具体值
 */
ChannelItemInfoDlg.set = function(key, val) {
    if(key == 'radios') {
        if($("#" + key).attr("radios") == null || $("#" + key).attr("radios") == "undefined") {
            var checkedRadioIds = "";
            $("input:checkbox[name='radio_name']:checked").each(function(i) {
                if(i == 0) {
                    checkedRadioIds = $(this).val();
                } else {
                    checkedRadioIds += "," + $(this).val();
                }

            });
            $("#" + key).attr("radios",checkedRadioIds);
        }
        this.ChanelItemInfoData[key] = (typeof value == "undefined") ? $("#" + key).attr("radios") : value;
    } else if(key =="channels") {
        this.ChanelItemInfoData[key] = (typeof value == "undefined") ? $("#" + key + " option:selected").val() : value;
    } else {
        this.ChanelItemInfoData[key] = (typeof value == "undefined") ? $("#" + key).val() : value;
    }
    return this;
}

/**
 * 设置对话框中的数据
 *
 * @param key 数据的名称
 * @param val 数据的具体值
 */
ChannelItemInfoDlg.get = function(key) {
    return $("#" + key).val();
}

/**
 * 关闭此对话框
 */
ChannelItemInfoDlg.close = function() {
    parent.layer.close(window.parent.ChannelItem.layerIndex);
};

/**
 * 收集数据
 */
ChannelItemInfoDlg.collectData = function() {
    this.set('id').set('channels').set('radios');
};

/**
 * 提交添加视频
 */
ChannelItemInfoDlg.addSubmit = function() {

    this.clearData();
    this.collectData();

    //提交信息
    var ajax = new $ax(Feng.ctxPath + "/channel/item/add", function(data){
        Feng.success("添加成功!");
        window.parent.ChannelItem.table.refresh();
        ChannelItemInfoDlg.close();
    },function(data){
        Feng.error("添加失败!" + data.responseJSON.message + "!");
    });
    ajax.set(this.ChanelItemInfoData);
    ajax.start();
};